Package de.yaams.extensions.basemap.tiled.plugins.tmw

Source Code of de.yaams.extensions.basemap.tiled.plugins.tmw.WLKWriter

/*
* The Mana World Plugin for Tiled, (c) 2004-2006
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* Adam Turk <aturk@biggeruniverse.com> Bjorn Lindeijer <bjorn@lindeijer.nl>
*/

package de.yaams.extensions.basemap.tiled.plugins.tmw;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

import de.yaams.extensions.basemap.tiled.core.Map;
import de.yaams.extensions.basemap.tiled.core.MapLayer;
import de.yaams.extensions.basemap.tiled.core.Tile;
import de.yaams.extensions.basemap.tiled.core.TileLayer;

/**
* The WLK file writer. The format is very simple:
*
* <pre>
*  short (width)
*  short (height)
*  char[] (data)
* </pre>
*
* @version $Id$
*/
public class WLKWriter {
  private static final int FIRST_BYTE = 0x000000FF;

  public static void writeMap(Map map, OutputStream out) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    MapLayer layer = null;

    // Get the last "collision" layer
    for (MapLayer mapLayer : map) {
      if (mapLayer.getName().equalsIgnoreCase("collision")) {
        layer = mapLayer;
      }
    }

    if (layer != null && layer instanceof TileLayer) {
      int width = layer.getWidth();
      int height = layer.getHeight();

      // Write width and height
      out.write(width & FIRST_BYTE);
      out.write(width >> 8 & FIRST_BYTE);
      out.write(height & FIRST_BYTE);
      out.write(height >> 8 & FIRST_BYTE);

      for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
          Tile tile = ((TileLayer) layer).getTileAt(x, y);
          if (tile != null && tile.getId() > 0) {
            out.write(1);
          } else {
            out.write(0);
          }
        }
      }

      baos.writeTo(out);
    } else {
      throw new Exception("No collision layer found!");
    }
  }
}
TOP

Related Classes of de.yaams.extensions.basemap.tiled.plugins.tmw.WLKWriter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.